﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.Statistics;
using MINERP.DAL;
using MINERP.COM;
using MINERP.MODEL.FoundationModel;

namespace MINERP.BLL
{
    public class PerforMoneyBll
    {
        public delegate void ShowMessage(string str);//定义委托将处理信息传递到表示层
        public delegate void ShowProcess(int currInt);//定义委托将处理进度传递到表示层

        public event ShowMessage ShowMsgHandle;
        public event ShowProcess ShowCurrPointHandle;
        public event ShowProcess ShowCountHandle;
        /// <summary>
        /// 查找业绩计算金额
        /// </summary>
        /// <param name="inStartTime"></param>
        /// <param name="inEndTime"></param>
        /// <param name="inUserList"></param>
        /// <returns></returns>
        public List<PerforMoneyInfo> GetStatisticMoney(DateTime inStartTime, DateTime inEndTime, List<int> inUserList)
        {
            ShowMsgHandle("正在获取数据");
            List<PerforMoneyInfo> rtnList = new List<PerforMoneyInfo>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                db.CommandTimeout = 6000;
                List<New_EmployeePerformance> listAllUser = new List<New_EmployeePerformance>();

                foreach (int userid in inUserList)
                {
                    string name = db.users.FirstOrDefault(p => p.userID == userid).UserName;
                    ShowMsgHandle("获取" + name + "的数据");
                    List<New_EmployeePerformance> userList = db.New_EmployeePerformance.Where(p => p.UserId == userid && p.OrderConpletedTime > inStartTime && p.OrderConpletedTime < inEndTime).ToList();
                    listAllUser.AddRange(userList);
                }

                int PrintType = (int)Bill_Detail_Detail_Type.NeedStatArea;
                int ProductType = (int)Bill_Detail_Detail_Type.UnNeedStatArea;
                int DesigntType = (int)Bill_Detail_Detail_Type.Design;

                int iCount = 0;
                int allCount = listAllUser.Count();
                ShowMsgHandle("获取到 " + allCount + " 条待处理的数据");
                ShowCountHandle(allCount);
                foreach (New_EmployeePerformance ne in listAllUser)
                {
                    ShowMsgHandle("正在处理" + iCount + "/" + allCount + "  产品 " + ne.OrderProductName);
                    ShowCurrPointHandle(iCount);
                    int orderproductid = ne.OrderproductId;
                    PerforMoneyInfo pm = new PerforMoneyInfo();
                    pm.CtmName = ne.CtmName;
                    pm.MultiplePageNum = ne.MultiplePageNum;
                    pm.OrderArea = ne.OrderArea;
                    pm.OrderCompletedTime = (DateTime)ne.OrderConpletedTime;
                    pm.OrderCount = ne.OrderCount;
                    pm.OrderCreateTime = ne.OrderCreateTime;
                    pm.OrderPath = ne.OrderPath;
                    pm.OrderProductId = ne.OrderproductId;
                    pm.OrderProductName = ne.OrderProductName;
                    pm.PhotoTotalNum = ne.PhotoTotalNum;
                    pm.PreOrderArea = ne.PreOrderAreas;
                    pm.ProductPhotoNum = ne.ProductPhotoNum;
                    pm.ReceivedTime = (DateTime)ne.ReceivedTime;
                    pm.ShopName = ne.ShopName;
                    pm.SinglePageNum = ne.SinglePageNum;
                    pm.Status = ne.Status;
                    pm.TechCompletedTime = ne.TechCompletedTime;
                    pm.TechId = ne.TechId;
                    pm.TechName = db.tb_Tech.FirstOrDefault(pp => pp.TechId == ne.TechId).TechName;
                    pm.UserName = db.users.FirstOrDefault(pp => pp.userID == ne.UserId).UserName;
                    string orderProductCode = "2" + orderproductid.ToString().PadLeft(9, '0');

                    Bill_Detail bd = db.Bill_Detail.FirstOrDefault(p => p.OrderInfoCode == orderProductCode);
                    if (bd != null)
                    {
                        bd.Bill_Detail_Detail.Load();
                        pm.PrintAreas = bd.Bill_Detail_Detail.Where(o => o.Type == PrintType).Sum(p => p.Area);
                        pm.PrintMoney = bd.Bill_Detail_Detail.Where(o => o.Type == PrintType).Sum(p => p.Count);
                        pm.ProductMoney = bd.Bill_Detail_Detail.Where(o => o.Type == ProductType).Sum(p => p.Count);
                        pm.DisginMoney = bd.Bill_Detail_Detail.Where(o => o.Type == DesigntType).Sum(p => p.Count);
                        pm.OrderCount = (decimal)bd.Bill_Detail_Detail.Sum(p => p.Count);
                    }
                    rtnList.Add(pm);
                    iCount++;
                }

            }
            return rtnList;
        }

        /// <summary>
        /// 备份查找业绩计算金额
        /// </summary>
        /// <param name="inStartTime"></param>
        /// <param name="inEndTime"></param>
        /// <param name="inUserList"></param>
        /// <returns></returns>
        public List<PerforMoneyInfo> GetStatisticMoneyBK(DateTime inStartTime, DateTime inEndTime, List<int> inUserList)
        {
            ShowMsgHandle("正在获取数据");
            List<PerforMoneyInfo> rtnList = new List<PerforMoneyInfo>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                db.CommandTimeout = 6000;
                List<New_EmployeePerformance_BK> listAllUser = new List<New_EmployeePerformance_BK>();

                foreach (int userid in inUserList)
                {
                    string name = db.users.FirstOrDefault(p => p.userID == userid).UserName;
                    ShowMsgHandle("获取" + name + "的数据");
                    List<New_EmployeePerformance_BK> userList = db.New_EmployeePerformance_BK.Where(p => p.UserId == userid && p.OrderConpletedTime > inStartTime && p.OrderConpletedTime < inEndTime).ToList();
                    listAllUser.AddRange(userList);
                }

                int PrintType = (int)Bill_Detail_Detail_Type.NeedStatArea;
                int ProductType = (int)Bill_Detail_Detail_Type.UnNeedStatArea;
                int DesigntType = (int)Bill_Detail_Detail_Type.Design;

                int iCount = 0;
                int allCount = listAllUser.Count();
                ShowMsgHandle("获取到 " + allCount + " 条待处理的数据");
                ShowCountHandle(allCount);
                foreach (New_EmployeePerformance_BK ne in listAllUser)
                {
                    ShowMsgHandle("正在处理" + iCount + "/" + allCount + "  产品 " + ne.OrderProductName);
                    ShowCurrPointHandle(iCount);
                    int orderproductid = ne.OrderproductId;
                    PerforMoneyInfo pm = new PerforMoneyInfo();
                    pm.CtmName = ne.CtmName;
                    pm.MultiplePageNum = ne.MultiplePageNum;
                    pm.OrderArea = ne.OrderArea;
                    pm.OrderCompletedTime = (DateTime)ne.OrderConpletedTime;
                    pm.OrderCount = ne.OrderCount;
                    pm.OrderCreateTime = ne.OrderCreateTime;
                    pm.OrderPath = ne.OrderPath;
                    pm.OrderProductId = ne.OrderproductId;
                    pm.OrderProductName = ne.OrderProductName;
                    pm.PhotoTotalNum = ne.PhotoTotalNum;
                    pm.PreOrderArea = ne.PreOrderAreas;
                    pm.ProductPhotoNum = ne.ProductPhotoNum;
                    pm.ReceivedTime = (DateTime)ne.ReceivedTime;
                    pm.ShopName = ne.ShopName;
                    pm.SinglePageNum = ne.SinglePageNum;
                    pm.Status = ne.Status;
                    pm.TechCompletedTime = ne.TechCompletedTime;
                    pm.TechId = ne.TechId;
                    pm.TechName = db.tb_Tech.FirstOrDefault(pp => pp.TechId == ne.TechId).TechName;
                    pm.UserName = db.users.FirstOrDefault(pp => pp.userID == ne.UserId).UserName;
                    string orderProductCode = "2" + orderproductid.ToString().PadLeft(9, '0');

                    Bill_Detail bd = db.Bill_Detail.FirstOrDefault(p => p.OrderInfoCode == orderProductCode);
                    if (bd != null)
                    {
                        bd.Bill_Detail_Detail.Load();
                        pm.PrintAreas = bd.Bill_Detail_Detail.Where(o => o.Type == PrintType).Sum(p => p.Area);
                        pm.PrintMoney = bd.Bill_Detail_Detail.Where(o => o.Type == PrintType).Sum(p => p.Count);
                        pm.ProductMoney = bd.Bill_Detail_Detail.Where(o => o.Type == ProductType).Sum(p => p.Count);
                        pm.DisginMoney = bd.Bill_Detail_Detail.Where(o => o.Type == DesigntType).Sum(p => p.Count);
                        pm.OrderCount = (decimal)bd.Bill_Detail_Detail.Sum(p => p.Count);
                    }
                    rtnList.Add(pm);
                    iCount++;
                }

            }
            return rtnList;
        }

        public List<PerforDeatailInfo> GetStatisticPerforDetail(DateTime inStartTime, DateTime inEndTime, List<int> inUserList)
        {
            ShowMsgHandle("正在获取数据");
            List<PerforDeatailInfo> rtnList = new List<PerforDeatailInfo>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                db.CommandTimeout = 6000;
                List<New_EmployeePerformance> listAllUser = new List<New_EmployeePerformance>();
                DateTime stTime = inStartTime.AddDays(-15);
                DateTime endTime = inEndTime.AddDays(15);
                foreach (int userid in inUserList)
                {
                    string name = db.users.FirstOrDefault(p => p.userID == userid).UserName;
                    ShowMsgHandle("获取" + name + "的数据");
                    List<New_EmployeePerformance> userList = db.New_EmployeePerformance.Where(p => p.UserId == userid && p.TechCompletedTime > stTime && p.TechCompletedTime < endTime).ToList();
                    listAllUser.AddRange(userList);
                }

                int PrintType = (int)Bill_Detail_Detail_Type.NeedStatArea;
                int ProductType = (int)Bill_Detail_Detail_Type.UnNeedStatArea;
                int DesigntType = (int)Bill_Detail_Detail_Type.Design;

                int iCount = 0;
                int allCount = listAllUser.Count();
                ShowMsgHandle("获取到 " + allCount + " 条待处理的数据");
                ShowCountHandle(allCount);
                foreach (New_EmployeePerformance ne in listAllUser)
                {
                    ShowMsgHandle("正在处理" + iCount + "/" + allCount + "  产品 " + ne.OrderProductName);
                    ShowCurrPointHandle(iCount);
                    int orderproductid = ne.OrderproductId;

                    string orderProductCode = "2" + orderproductid.ToString().PadLeft(9, '0');

                    Bill_Detail bd = db.Bill_Detail.FirstOrDefault(p => p.OrderInfoCode == orderProductCode);
                    if (bd != null)
                    {
                        bd.SHOP_BillReference.Load();
                        if (bd.SHOP_Bill.CreateTime < inEndTime && bd.SHOP_Bill.CreateTime > inStartTime)
                        {
                            if (!bd.Bill_Detail_Detail.IsLoaded)
                            {
                                bd.Bill_Detail_Detail.Load();
                            }
                            foreach (Bill_Detail_Detail bdd in bd.Bill_Detail_Detail)
                            {
                                PerforDeatailInfo pm = new PerforDeatailInfo();
                                pm.CtmName = ne.CtmName;
                                pm.MultiplePageNum = ne.MultiplePageNum;
                                pm.OrderArea = ne.OrderArea;
                                pm.OrderCompletedTime = (DateTime)ne.OrderConpletedTime;
                                pm.OrderCount = ne.OrderCount;
                                pm.OrderCreateTime = ne.OrderCreateTime;
                                pm.OrderPath = ne.OrderPath;
                                pm.OrderProductId = ne.OrderproductId;
                                pm.OrderProductName = ne.OrderProductName;
                                pm.PhotoTotalNum = ne.PhotoTotalNum;
                                pm.PreOrderArea = ne.PreOrderAreas;
                                pm.ProductPhotoNum = ne.ProductPhotoNum;
                                pm.ReceivedTime = (DateTime)ne.ReceivedTime;
                                pm.ShopName = ne.ShopName;
                                pm.SinglePageNum = ne.SinglePageNum;
                                pm.Status = ne.Status;
                                pm.TechCompletedTime = ne.TechCompletedTime;
                                pm.TechId = ne.TechId;
                                pm.TechName = db.tb_Tech.FirstOrDefault(pp => pp.TechId == ne.TechId).TechName;
                                pm.UserName = db.users.FirstOrDefault(pp => pp.userID == ne.UserId).UserName;
                                pm.OrderProductId = ne.OrderproductId;

                                pm.Areas = bdd.Area;
                                pm.Count = bdd.Count;
                                pm.DetailName = bdd.DetailName;
                                pm.Num = bdd.Num;
                                pm.Spec = bdd.Spec;
                                pm.UnitPrice = bdd.UnitPrice;
                                if (bdd.Type == ProductType)
                                {
                                    pm.Type = "制作费";
                                }
                                else if (bdd.Type == PrintType)
                                {
                                    pm.Type = "输出费";
                                }
                                else if (bdd.Type == DesigntType)
                                {
                                    pm.Type = "设计费";
                                }

                                rtnList.Add(pm);
                            }
                        }
                    }
                    else
                    {
                        //rtnList.Add(pm);
                        continue;
                    }

                    iCount++;
                }

            }
            return rtnList;
        }
        /// <summary>
        /// 获取到备份中的金额
        /// </summary>
        /// <param name="inStartTime"></param>
        /// <param name="inEndTime"></param>
        /// <param name="inUserList"></param>
        /// <returns></returns>
        public List<PerforDeatailInfo> GetStatisticPerforDetailBK(DateTime inStartTime, DateTime inEndTime, List<int> inUserList)
        {
            ShowMsgHandle("正在获取数据");
            List<PerforDeatailInfo> rtnList = new List<PerforDeatailInfo>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                db.CommandTimeout = 6000;
                List<New_EmployeePerformance_BK> listAllUser = new List<New_EmployeePerformance_BK>();
                DateTime stTime = inStartTime.AddDays(-15);
                DateTime endTime = inEndTime.AddDays(15);
                foreach (int userid in inUserList)
                {
                    string name = db.users.FirstOrDefault(p => p.userID == userid).UserName;
                    ShowMsgHandle("获取" + name + "的数据");
                    List<New_EmployeePerformance_BK> userList = db.New_EmployeePerformance_BK.Where(p => p.UserId == userid && p.OrderConpletedTime > stTime && p.OrderConpletedTime < endTime).ToList();
                    listAllUser.AddRange(userList);
                }

                int PrintType = (int)Bill_Detail_Detail_Type.NeedStatArea;
                int ProductType = (int)Bill_Detail_Detail_Type.UnNeedStatArea;
                int DesigntType = (int)Bill_Detail_Detail_Type.Design;

                int iCount = 0;
                int allCount = listAllUser.Count();
                ShowMsgHandle("获取到 " + allCount + " 条待处理的数据");
                ShowCountHandle(allCount);
                foreach (New_EmployeePerformance_BK ne in listAllUser)
                {
                    ShowMsgHandle("正在处理" + iCount + "/" + allCount + "  产品 " + ne.OrderProductName);
                    ShowCurrPointHandle(iCount);
                    int orderproductid = ne.OrderproductId;
                    string orderProductCode = "2" + orderproductid.ToString().PadLeft(9, '0');

                    Bill_Detail bd = db.Bill_Detail.FirstOrDefault(p => p.OrderInfoCode == orderProductCode);

                    if (bd != null)
                    {
                        bd.SHOP_BillReference.Load();
                        if (bd.SHOP_Bill.CreateTime < inEndTime && bd.SHOP_Bill.CreateTime > inStartTime)
                        {
                            if (!bd.Bill_Detail_Detail.IsLoaded)
                            {
                                bd.Bill_Detail_Detail.Load();
                            }
                            foreach (Bill_Detail_Detail bdd in bd.Bill_Detail_Detail)
                            {
                                PerforDeatailInfo pm = new PerforDeatailInfo();
                                pm.CtmName = ne.CtmName;
                                pm.MultiplePageNum = ne.MultiplePageNum;
                                pm.OrderArea = ne.OrderArea;
                                pm.OrderCompletedTime = (DateTime)ne.OrderConpletedTime;
                                pm.OrderCount = ne.OrderCount;
                                pm.OrderCreateTime = ne.OrderCreateTime;
                                pm.OrderPath = ne.OrderPath;
                                pm.OrderProductId = ne.OrderproductId;
                                pm.OrderProductName = ne.OrderProductName;
                                pm.PhotoTotalNum = ne.PhotoTotalNum;
                                pm.PreOrderArea = ne.PreOrderAreas;
                                pm.ProductPhotoNum = ne.ProductPhotoNum;
                                pm.ReceivedTime = (DateTime)ne.ReceivedTime;
                                pm.ShopName = ne.ShopName;
                                pm.SinglePageNum = ne.SinglePageNum;
                                pm.Status = ne.Status;
                                pm.TechCompletedTime = ne.TechCompletedTime;
                                pm.TechId = ne.TechId;
                                pm.TechName = db.tb_Tech.FirstOrDefault(pp => pp.TechId == ne.TechId).TechName;
                                pm.UserName = db.users.FirstOrDefault(pp => pp.userID == ne.UserId).UserName;
                                ;
                                pm.Areas = bdd.Area;
                                pm.Count = bdd.Count;
                                pm.DetailName = bdd.DetailName;
                                pm.Num = bdd.Num;
                                pm.Spec = bdd.Spec;
                                pm.UnitPrice = bdd.UnitPrice;
                                if (bdd.Type == ProductType)
                                {
                                    pm.Type = "制作费";
                                }
                                else if (bdd.Type == PrintType)
                                {
                                    pm.Type = "输出费";
                                }
                                else if (bdd.Type == DesigntType)
                                {
                                    pm.Type = "设计费";
                                }

                                rtnList.Add(pm);
                            }
                        }
                        else
                        {
                            // rtnList.Add(pm);
                        }
                    }
                    else
                    {

                    }

                    iCount++;
                }

            }
            return rtnList;
        }


        /// <summary>
        /// 获取所有组
        /// </summary>
        /// <returns></returns>
        public List<Tb_WorkerGroup_Info> GetAllGroup()
        {
            List<Tb_WorkerGroup_Info> rtnList = new List<Tb_WorkerGroup_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<tb_WorkerGroup> listGroup = db.tb_WorkerGroup.ToList();

                Tb_WorkerGroup_Info frist = new Tb_WorkerGroup_Info();
                frist.GroupId = 0;
                frist.GroupName = "全部";
                rtnList.Add(frist);
                foreach (tb_WorkerGroup group in listGroup)
                {
                    Tb_WorkerGroup_Info info = new Tb_WorkerGroup_Info() { GroupId = group.GroupId, GroupName = group.GroupName };
                    rtnList.Add(info);
                }
            }
            return rtnList;
        }
        public List<UserInfo> GetUserListByGroupId(int GroupId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                if (GroupId == 0)
                {
                    return ConvertUserToInfo(db.users.ToList());
                }
                else
                {
                    return ConvertUserToInfo(db.users.Where(u => u.tb_WorkerGroup.GroupId == GroupId && u.IsUseful == true).ToList());
                }
            }
        }
        private List<UserInfo> ConvertUserToInfo(List<users> uList)
        {
            List<UserInfo> result = new List<UserInfo>();

            foreach (var u in uList)
            {
                result.Add(new UserInfo()
                {
                    UserID = u.userID,
                    UserName = u.UserName

                });
            }

            return result;
        }

    }
}
